package chat.server;

import java.util.*;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ObjectOutputStream;
import java.net.Socket;

import chat.user.Group;
import chat.user.Message;
import chat.user.MessageCreator;
import chat.user.User;

public class SocketProcessor implements Runnable {
	
	private Map<String, Socket> socket_list;
	private ObjectOutputStream oos;
	private boolean REQUEST_AUTENTIFICATION=false;
	private static Queue<Message> OutQueue = new LinkedList<Message>();
	private Message m;
	Object sync = new Object();
	
	public SocketProcessor(){
		
		
	}
	
	@Override
	public synchronized void run(){
			
		try {
			
			while (true){
			
				synchronized (sync){
				//Thread.sleep(500);
				
				while (MultiThread.getCountInQueue()>0) {
					
					m = MultiThread.getInMessage();
					
					if (m.GROUP_UPDATE){
						
						//m.setGroupNames(GroupProcessor.getGroupList());
						
						/*String n = m.getUser().getUserName();
						
						HashMap<String, Socket> socket_list = GroupProcessor.getGroup(m.getUser().getRelatedGroup()).getUserSocketList();
						
						Socket s = null;
						
							for (Map.Entry<String, Socket> entry: socket_list.entrySet()){
								
								if (n.equalsIgnoreCase(entry.getKey())){
									
									s = entry.getValue(); break;
								}
								
							}
						*/		
						
						String relatedGroup = m.getUser().getRelatedGroup();
						
						Group p1 = GroupProcessor.getGroup(relatedGroup);
						
						Socket s = p1.getSocket(m.getUser());
						
						Message response_message = MessageCreator.GroupUpdateRequestResponse(s, GroupProcessor.getGroupList());
												
						addMessageOutQueue(response_message);
						
					}
					if (m.SEND_MESSAGE){
						
						Group g = GroupProcessor.getGroup(m.getUser().getRelatedGroup());
						
						Vector<User> user_list = g.getUserList();
						
						for (User u: user_list){
						
							addMessageOutQueue(MessageCreator.sendTextMessage(m.getMessage(),u.getSocket()));
							
						}
					}
					if (m.NEW_GROUP_REQUEST){
						
						GroupProcessor.addToGroupList(m.getNewGroup());
						
						Vector<Group> group_list = GroupProcessor.getGroupList();
						
						for (int i=0; i<group_list.size(); i++){
							
							Vector<User> user_list = group_list.get(i).getUserList();
							
							for (User u: user_list){
								
								addMessageOutQueue(MessageCreator.GroupUpdateRequestResponse(u.getSocket(), GroupProcessor.getGroupList()));
							}
						}
					}
					
					if (m.CONNECT_TO_GROUP){
						
						GroupProcessor.connectToGroup(m.getGroup(), m.getUser().getUserName());
						
					//	Message OutMessage = new Message (m.getGroup(), m.getUser());
						
						String name = m.getUser().getUserName();
						
						Vector<User> user_list = GroupProcessor.getGroup(m.getGroup()).getUserList();
						
						for (User u: user_list){
						
							if (name.equalsIgnoreCase(u.getUserName())){
								
								addMessageOutQueue(MessageCreator.ConnectGroupRequestResponse(u.getSocket()));
								
							}
							
						}
					}
					
					MultiThread.RemoveFromInQueue(m);
				}
				}
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
			
	}
	
	public static boolean addMessageOutQueue(Message m){
		
		return OutQueue.add(m);
	}
	public static boolean removeMessageOutQueue(Message m){
		
		return OutQueue.remove(m);
	}
	public static int getCountOutQueue(){
		
		return OutQueue.size();
	}
	public static Message getMessage(){
		
		return OutQueue.peek();
		
	}
	
}
